﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StringExtensions.cs" company="AzureContrib (Open source library http://azurecontrib.codeplex.com)">
//   Microsoft Public License (Ms-PL)
// </copyright>
// <summary>
//   Extensions on the <see cref="string" /> class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Diagnostics.CodeAnalysis;

namespace AzureContrib.WindowsAzure
{
	/// <summary>
	/// Extensions on the <see cref="string"/> class.
	/// </summary>
	public static class StringExtensions
	{
		/// <summary>
		/// Validates a url
		/// </summary>
		/// <param name="toValidate">The url to validate</param>
		/// <returns>True if the <paramref name="toValidate"/> is a valid <see cref="Uri"/>.</returns>
		public static bool IsValidUrl(this string toValidate)
		{
			if (string.IsNullOrEmpty(toValidate))
			{
				return false;
			}

			if (!toValidate.StartsWith("http", StringComparison.Ordinal))
			{
				toValidate = string.Concat("http://", toValidate);
			}

			Uri uri;
			return Uri.TryCreate(toValidate, UriKind.Absolute, out uri);
		}

		/// <summary>
		/// Calls <see cref="string.Format(string,object[])"/>.
		/// </summary>
		/// <param name="format">
		/// The format.
		/// </param>
		/// <param name="args">
		/// The args.
		/// </param>
		/// <returns>
		/// The formatted string.
		/// </returns>
		[SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object[])", Justification = "The idea here is to actually forward to the string.format version that does not use IFormatProvider."), 
		 SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object,System.Object)", Justification = "The idea here is to actually forward to the string.format version that does not use IFormatProvider."),
		 SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "F", Justification = "The whole point of this function is to be short.")]
		public static string F(this string format, params object[] args)
		{
			return string.Format(format, args);
		}

		/// <summary>
		/// Calls <see cref="string.Format(IFormatProvider,string,object[])"/>.
		/// </summary>
		/// <param name="format">
		/// The format.
		/// </param>
		/// <param name="provider">
		/// The provider.
		/// </param>
		/// <param name="args">
		/// The args.
		/// </param>
		/// <returns>
		/// The formatted string.
		/// </returns>
		[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "F", Justification = "The whole point of this function is to be short.")]
		public static string F(this string format, IFormatProvider provider, params object[] args)
		{
			return string.Format(provider, format, args);
		}

		/// <summary>
		/// Calls <see cref="string.Format(string,object)"/>.
		/// </summary>
		/// <param name="format">
		/// The string to format.
		/// </param>
		/// <param name="arg">
		/// The argument.
		/// </param>
		/// <returns>
		/// The formatted string.
		/// </returns>
		[SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object)", Justification = "The idea here is to actually forward to the string.format version that does not use IFormatProvider."),
		 SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "F", Justification = "The whole point of this function is to be short.")]
		public static string F(this string format, object arg)
		{
			return string.Format(format, arg);
		}

		/// <summary>
		/// Calls <see cref="string.Format(string,object[])"/>.
		/// </summary>
		/// <param name="format">
		/// The format.
		/// </param>
		/// <param name="arg">
		/// The arg 0.
		/// </param>
		/// <param name="arg1">
		/// The arg 1.
		/// </param>
		/// <returns>
		/// The formatted string.
		/// </returns>
		[SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object,System.Object)", Justification = "The idea here is to actually forward to the string.format version that does not use IFormatProvider."),
		 SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "F", Justification = "The whole point of this function is to be short.")]
		public static string F(this string format, object arg, object arg1)
		{
			return string.Format(format, arg, arg1);
		}

		/// <summary>
		/// Calls <see cref="string.Format(string,object,object,object)"/>.
		/// </summary>
		/// <param name="format">The format.</param>
		/// <param name="arg">The arg 0.</param>
		/// <param name="arg1">The arg 1.</param>
		/// <param name="arg2">The arg 2.</param>
		/// <returns>The formatted string.</returns>
		[SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object,System.Object,System.Object)", Justification = "The idea here is to actually forward to the string.format version that does not use IFormatProvider."), 
		 SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object,System.Object)", Justification = "The idea here is to actually forward to the string.format version that does not use IFormatProvider."),
		 SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "F", Justification = "The whole point of this function is to be short.")]
		public static string F(this string format, object arg, object arg1, object arg2)
		{
			return string.Format(format, arg, arg1, arg2);
		}
	}
}